package com.nc.cloud.admin.rest;

import com.nc.cloud.admin.biz.MenuBiz;
import com.nc.cloud.admin.biz.RoleMenuBiz;
import com.nc.cloud.admin.entity.Menu;
import com.nc.cloud.admin.entity.RoleMenu;
import com.nc.cloud.common.context.BaseContextHandler;
import com.nc.cloud.common.msg.ObjectRestResponse;
import com.nc.cloud.common.rest.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("menu")
public class MenuController extends BaseController<MenuBiz, Menu> {


    @Autowired
    MenuBiz menuBiz;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<Menu> list(String parentCode) {
        if (StringUtils.isBlank(parentCode)) {
            return null;
        }
        Example example = new Example(Menu.class);
        example.createCriteria().andEqualTo("parentCode", parentCode);
        example.setOrderByClause("sort asc");
        return baseBiz.selectByExample(example);
    }

    @RequestMapping(value = "/addMenu", method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<Menu> addMenu(@RequestBody Menu menu) {
        if (menu == null || StringUtils.isBlank(menu.getName()) || StringUtils.isBlank(menu.getLocaleEn())) {
            return null;
        }
        return new ObjectRestResponse<>().data(menuBiz.addMenu(menu));
    }

    @RequestMapping(value = "/del/{code}",method = RequestMethod.DELETE)
    @ResponseBody
    public ObjectRestResponse<String> del(@PathVariable String code){
        return new ObjectRestResponse<String>().data(menuBiz.deleteAllByCode(code));
    }

    @RequestMapping(value = "/listtree", method = RequestMethod.GET)
    @ResponseBody
    public List<Menu> listCondation(HashMap Condation) {

        Example example = new Example(Menu.class);
        example.createCriteria().andEqualTo("parentCode", Condation.get("parentCode"));
        example.setOrderByClause("sort asc");
        return baseBiz.selectByExample(example);
    }


}